﻿using Macad.Core;
using Macad.Core.Shapes;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Interaction.Dialogs;
using Macad.Occt;
using static Macad.Exchange.StepExchanger;
using static Macad.Interaction.ViewportController;

namespace SpCad
{
    public interface ISpView
    {
        public sFile File { get; }
        public   InteractiveContext Context => InteractiveContext.Current;
        /// <summary>
        /// 锁定视图
        /// </summary>
        public bool IsLookPlane { get => InteractiveContext.Current.ViewportController.LockedToPlane; set => InteractiveContext.Current.ViewportController.LockedToPlane = value; }
        /// <summary>
        /// Undo提交修改
        /// </summary>
        public void Commit() => InteractiveContext.Current.UndoHandler.Commit();
        /// <summary>
        /// 显示消息
        /// </summary>
        /// <param name="str"></param>
        public void Info(string str)
        {
            
            Messages.Info(str); 
        }
        /// <summary>
        /// 缩放到显示所有
        /// </summary>
        public void ZoomFitAll()
        {
            InteractiveContext.Current?.ViewportController?.ZoomFitAll();
        }
        /// <summary>
        /// 缩放比例
        /// </summary>
        /// <param name="v"></param>
        public void ZoomOut(double v)
        {
            InteractiveContext.Current?.ViewportController?.Zoom(v);
        }
        /// <summary>
        /// 显示到模型上下左右
        /// </summary>
        /// <param name="predefinedView"></param>
        public void SetPredefinedView(PredefinedViews predefinedView)
        {
            InteractiveContext.Current.ViewportController.SetPredefinedView(predefinedView);
        }

        public void Select()
        {
            Body body = InteractiveContext.Current.WorkspaceController.Selection.SelectedEntities.FirstOrDefault() as Body; ;
            var toolAction = new SelectSubshapeAction(SubshapeTypes.Face, body);
            toolAction.Finished += ToolAction_Finished;
 
        }

        void ToolAction_Finished(SelectSubshapeAction sender, SelectSubshapeAction.EventArgs args);
        public void Import()
        {
            var WorkingPlaneLocation = SpServer.Instance.WorkingPlane.Location;
            if (ImportDialog.Execute<IBodyImporter>(out var filename, out var importer))
            {
 
                if (importer.Extensions[0]== "step")
                {
                  
                    ((StepSettings)importer.Settings).ImportSingleBody = true;
                }
              
                if (!importer.DoImport(filename, out var newBodies))
                {
                    ErrorDialogs.CannotImport(filename);
                    return;
                } 
                foreach (var newBody in newBodies)
                {
                    
                 //  newBody.Position = WorkingPlaneLocation;
                    
                   CoreContext.Current?.Document?.Add(newBody);
              
                    SetPoint(newBody);
                }
                
                InteractiveContext.Current.UndoHandler.Commit();
                InteractiveContext.Current.WorkspaceController?.Selection.SelectEntities(newBodies);
                ZoomFitAll();
            }

        }
        private void SetPoint(Body? body)
        {
            // InteractiveContext.Current.Layers.Add(new Layer {  Guid=1 });
            if (InteractiveContext.Current.Layers.Count() < 2)
            {
                InteractiveContext.Current.Layers.Add(new Layer {Name="1(工件)", LineThickness=  LineThickness.Thick });
            }
            if (body != null)
            {
                body.Layer = InteractiveContext.Current.Layers.Last();
                SetZeroPonit(body);
            }
         
            return;
   
        }
        /// <summary>
        /// 设置零点位置
        /// </summary>
        public void SetZeroPonit(Body? body)
        {

            var loc = SpServer.Instance.WorkingPlane.Location;
            TopoDS_Shape ShapeBrep = body.GetBRep();
            AIS_Shape aisShape = new AIS_Shape(ShapeBrep);
            var BoundBox = ShapeBrep.BoundingBox();
            var BoxMini = BoundBox.CornerMin();

            Vec tranvec = new Vec(BoxMini, loc);
            body.Position = new Pnt(tranvec.X, tranvec.Y, tranvec.Z);
        }
    }
}
